package com.naza.rpc.register;

import com.alibaba.nacos.api.naming.NamingFactory;
import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.api.naming.listener.Event;
import com.alibaba.nacos.api.naming.listener.EventListener;
import com.alibaba.nacos.api.naming.listener.NamingEvent;
import com.alibaba.nacos.api.naming.pojo.Instance;

import java.util.List;

public class NacosDiscoveryDemo {
    public static void main(String[] args) {
        try {
            NamingService naming = NamingFactory.createNamingService("localhost:8848");
            System.out.println(naming.getAllInstances("nacos.test.4"));
            System.out.println(naming.getAllInstances("nacos.test.3"));
            naming.subscribe("nacos.test.4", new EventListener() {
                @Override
                public void onEvent(Event event) {
                    NamingEvent e = (NamingEvent)event;
                    List<Instance> all = e.getInstances();
                    System.out.println(all);
                }
            });
        }catch (Exception e){

        }
        // 测试让主线程不退出，因为订阅配置是守护线程，主线程退出守护线程就会退出。 正式代码中无需下面代码
        while (true) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }
}
